﻿#if ENABLE_ET_COM
#define ENABLE_AutoItX
namespace suit_bigmem
{
    using System;
    using System.IO;
    using System.Collections.Generic;
    class CBigMem : suit_base.SuiteBase
    {
        private autoIt.CAutoIt m_wndKiller;
        private CExpFile m_expFile;
        private string m_sSample;
        private string m_sOutput;
        //private const long LIMIT_MEM = 100000;
        private const long LIMIT_MEM = 50000;

        public override string GetCommand()
        {
            return ET_Improve.Global.CMD_BIGMEM;
        }

        public override void Call()
        {
            try
            {
                LoadConfig();
                Dictionary<string, List<tool.CAddInfoHlp.CFileInfo>> fileDic =
                    tool.CAddInfoHlp.GetFolderFiles(m_sSample, ET_Improve.Global.CMD_BIGMEM);

                m_wndKiller = new autoIt.CAutoIt();
                m_wndKiller.Start();

                m_expFile = new CExpFile();
                m_expFile.Begin(m_sOutput);

                int nCurDir = 0;
                int nDirs = fileDic.Keys.Count;
                foreach (string sDir in fileDic.Keys)
                {
                    ++nCurDir;
                    int i = 0;
                    int nCount = fileDic[sDir].Count;
                    if(nCount == 0)
                    {
                        Console.WriteLine(string.Format("total-dirs:{0:D}/{1:D}", nCurDir, nDirs));
                        continue;
                    }
                    tool.LogTool.GetInstance().BeginUseLog(sDir, ET_Improve.Global.s_curCmdName);
                    m_expFile.BeginPath(sDir, nCount);
                    foreach (tool.CAddInfoHlp.CFileInfo fi in fileDic[sDir])
                    {
                        double d = (double)(++i) / (double)nCount;
                        Console.WriteLine(string.Format("->cur-task:{0:D}/{1:D}  {2:P}\t total-dirs:{3:D}/{4:D}",
                            i, nCount, d, nCurDir, nDirs));
                        OpenTest(fi);
                    }
                    m_expFile.EndPath();
                    tool.LogTool.GetInstance().EndUseLog();
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                m_wndKiller.Stop();
                m_expFile.End();
            }
        }

        protected void LoadConfig()
        {
            const string LOG_ROOT = "big-mem";
            const string LOG_SAMPLE = "sample-path";
            const string LOG_OUTPUT = "output-file";
            try
            {
                foreach (System.Xml.XmlNode nodeX in tool.ConfigReader.GetElementList(LOG_ROOT))
                {
                    if (nodeX.Name == LOG_SAMPLE)
                    {
                        m_sSample = nodeX.InnerText;
                    }
                    else if (nodeX.Name == LOG_OUTPUT)
                    {
                        m_sOutput = nodeX.InnerText;
                    }
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
        }

        protected void OpenTest(tool.CAddInfoHlp.CFileInfo fi)
        {
            long lMem = 0;
            etOp.EtOp pET = new etOp.EtOp();
            try
            {
                m_wndKiller.BeginMonitor(true);
                pET.Init(m_wndKiller, true);
                pET.Open(fi.FileName, fi.Password);
                string sFileName = Path.GetFileName(fi.FileName);
                Console.WriteLine(sFileName);
                int nNotUsed = 0;
                if (pET.GetTimeMemory(ref nNotUsed, ref lMem))
                {
                    if(lMem > LIMIT_MEM)
                    {
                        m_expFile.AddRecord(sFileName, lMem);
                    }
                }
                pET.Close();
            }
            catch(Exception ex)
            {
                tool.LogTool.GetInstance().AddLog(tool.LogTool.LOG_TYPE.EXCEPTION, fi.FileName, null, ex);
            }
            finally
            {
                m_wndKiller.EndMonitor();
                etOp.EtOp.DestoryET(ref pET);
            }
        }
    }

    internal class CExpFile
    {
        StreamWriter m_sw;
        private string m_sCache;
        private int m_nFind;
        public void Begin(string sOutput)
        {
            m_sw = new StreamWriter(sOutput);
            m_nFind = 0;
        }

        public void AddRecord(string sFileName, long lMem)
        {
            if(0 == m_nFind)
            {
                m_sw.WriteLine(m_sCache);
            }
            ++m_nFind;
            m_sw.WriteLine(string.Format("{0}\t{1}", Path.GetFileName(sFileName), lMem));
        }

        public void End()
        {
            if (m_sw != null)
            {
                m_sw.Close();
                m_sw = null;
            }
        }

        public void BeginPath(string sDir, int nFiles)
        {
            m_sCache = string.Format("[{0} : {1}]", sDir, nFiles);
            Console.WriteLine(">BeginPath:" + sDir);
        }

        public void EndPath()
        {
            if(m_nFind > 0)
            {
                Console.WriteLine();
                m_sw.WriteLine();
            }
        }
    }
}
#endif // #if ENABLE_ET_COM